//
//  NewsImageListViewController.m
//  Movie
//
//  Created by mycomputer on 15/7/22.
//  Copyright (c) 2015年 杨韦智. All rights reserved.
//

#import "NewsImageListViewController.h"
#import "DataService.h"
#import "Base.h"
#import "NewsImageCollectionViewCell.h"
#import "PhotoViewController.h"
#import "BaseNavigationController.h"


@interface NewsImageListViewController ()

@end

@implementation NewsImageListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self _createCollectionViewController];
    [self _loadData];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - loadData
- (void)_loadData {
    _newsImageListArray = [[NSMutableArray alloc] init];
    NSArray *imageArray = [DataService getJsonDataFromFile:newsImageList];
    for (NSDictionary *dic in imageArray) {
        NewsImageModel *model = [[NewsImageModel alloc] init];
        [model setValuesForKeysWithDictionary:dic];
        [_newsImageListArray addObject:model];
    }
}

#pragma mark - createSubViewController
- (void)_createCollectionViewController {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((kWidth-50)/4, (kWidth-50)/4);
    
    _newsImageListCollectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    
    _newsImageListCollectionView.dataSource = self;
    _newsImageListCollectionView.delegate = self;
    
    [_newsImageListCollectionView registerClass:[NewsImageCollectionViewCell class] forCellWithReuseIdentifier:@"newsImageCell"];
    
    [self.view addSubview:_newsImageListCollectionView];
}

#pragma mark - UICollectionViewDataSource,UICollectionViewDelegateFlowLayout
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _newsImageListArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    NewsImageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"newsImageCell" forIndexPath:indexPath];
    cell.model = _newsImageListArray[indexPath.row];
    return cell;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(10, 10, 10, 10);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    PhotoViewController *vc = [[PhotoViewController alloc] init];
    
    
    
    //抽离url
    NSMutableArray *imageUrlArray = [[NSMutableArray alloc] init];
    for (NSInteger i = 0; i<_newsImageListArray.count; i++) {
        NewsImageModel *model = _newsImageListArray[i];
        NSString *imageUrl = model.image;
        [imageUrlArray addObject:imageUrl];
    }
    
    vc.imageUrlArray = imageUrlArray;
    
    vc.currentIndex = indexPath.row;
    
   // vc.view.backgroundColor = [UIColor clearColor];
    
    BaseNavigationController *nav = [[BaseNavigationController alloc] initWithRootViewController:vc];
    
    [self presentViewController:nav animated:YES completion:nil];
}




/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
